<template>
	<view class="content">
		<!--  #ifdef APP-PLUS -->
		<view class="top-bar"></view>
		<!--  #endif -->
		<view class="custom">
			<image class="custom-bj" src="@/static/images/news/background.png"></image>
			<view class="custom-title">消息</view>
			<view class="custom-del" @tap="delNews">
				<image class="custom-bj" src="@/static/images/news/del.svg"></image>
			</view>
		</view>
		<view class="revenue">
			<view class="revenue-title">
				<view class="revenue-title__t">
					<span class="blcok"></span>
					每日任务
				</view>
				<view class="revenue-title__more" @tap="$Router.push('/pages/integral/task')">
					更多任务<text class="iconfont icon-xiayiye"></text>
				</view>
			</view>
			<view class="revenue-list">
				<view v-if="taskList.length > 0">
					<block v-for="(item, index) in taskList" :key="index">
						<task-li :item="item" @refresh="taskRefresh" @sign="goSign"></task-li>
					</block>
				</view>
				<LoadMore v-else :empty="true" emptyText="暂无任务"></LoadMore>
			</view>
		</view>

		<view class="activity" v-if="revenueList.length > 0">
			<view class="activity-title">
				<view class="activity-title__t">
					<span class="blcok"></span>
					收入消息
				</view>
				<view class="activity-title__more" @tap="toRevenue">
					查看更多<text class="iconfont icon-xiayiye"></text>
				</view>
			</view>
			<view class="activity-list">
				<view v-if="revenueList.length > 0">
					<block v-for="(item, index) in revenueList" :key="index">
						<revenue-li :item="item"></revenue-li>
					</block>
				</view>
				<LoadMore v-else :empty="true" emptyText="暂无消息"></LoadMore>
			</view>
			<image class="revenue-bj" src="@/static/images/news/box-bj.svg"></image>
		</view>
		<view class="activity">
			<view class="activity-title">
				<view class="activity-title__t">
					<span class="blcok"></span>
					活动消息
				</view>
				<view class="activity-title__more" @tap="toActivity">
					查看更多<text class="iconfont icon-xiayiye"></text>
				</view>
			</view>
			<view class="activity-list">
				<view v-if="activityList.length > 0">
					<block v-for="(item, index) in activityList" :key="index">
						<activity-li :item="item"></activity-li>
					</block>
				</view>
				<LoadMore v-else :empty="true" emptyText="暂无消息"></LoadMore>
			</view>
		</view>
		<view class="user" @tap="$Router.push('/pages/member/service')">
			<image class="user-img" src="@/static/images/news/icon-user.svg"></image>
			<text class="value">联系客服</text>
		</view>
		<!--智能搜索-->
		<ai-search ref="aiSearch"></ai-search>
	</view>
</template>

<script>
import ActivityLi from '@/components/news/ActivityLi';
import RevenueLi from '@/components/news/RevenueLi';
import LoadMore from '@/components/LoadMore';
import TaskLi from '@/components/integral/TaskLi';

export default {
	data() {
		return {
			revenueList: [],
			activityList: [],
			taskList: [],
			loginBtnLock: false,
			todayHasSign: 0
		};
	},
	components: {
		ActivityLi,
		RevenueLi,
		LoadMore,
		TaskLi
	},
	onLoad() {
	},
	onShow() {
		this.$nextTick(() => {
			uni.getClipboardData({
				success: res => {
					if (res.data && this.$store.state.common.aiSearch) {
						this.$refs.aiSearch.open(res.data);
						this.$store.commit({
							type: 'setAiSearch',
							aiSearch: false
						});
					}
				}
			});
		});
		if (!uni.getStorageSync('token')) {
			if (!this.loginBtnLock) {
				this.loginBtnLock = true;
				uni.navigateTo({
					url: '/pages/common/login?intention=index',
					animationType: 'slide-in-bottom',
					animationDuration: 300
				});
			} else {
				this.loginBtnLock = false;
				this.$Router.pushTab('/pages/index/index');
			}

			return false;
		}
		this.refresh();
	},
	onHide() {
		this.$refs.aiSearch.close();
	},
	onReady() {
		// #ifdef H5
		this.stickyTop = 146;
		// #endif
	},

	methods: {
		delNews() {
			let that = this;
			that.$service.news.notices
				.updateAllStatus()
				.then(res => {
					uni.showToast({
						title: '清除成功',
						icon: 'none'
					});
					setTimeout(() => {
						that.getActivity();
					}, 1000);
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		refresh() {
			this.getTask();
			this.getActivity();
			this.getRevenue();
			uni.stopPullDownRefresh();
		},
		getTask() {
			this.$service.integral.score
				.page({
					page: 1,
					size: 1
				})
				.then(res => {
					this.taskList = res.list;
					this.getSignList();
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		getRevenue() {
			this.$service.news.bills
				.incomeList({
					page: 1,
					size: 2
				})
				.then(res => {
					this.revenueList = res.list;
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		getSignList() {
			this.$service.integral.score
				.signList()
				.then(res => {
					this.todayHasSign = res.todayHasSign;
					// 获取用户信息
					// #ifdef H5
					this.$store.dispatch('getUserInfo', { versionCode: 140 });
					// #endif
					// #ifdef APP-PLUS
					plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
						let params = { ...wgtinfo };
						this.$store.dispatch('getUserInfo', params);
					});
					// #endif
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		getActivity() {
			this.$service.news.notices
				.page({
					page: 1,
					size: 2
				})
				.then(res => {
					this.activityList = res.list;
					
					uni.hideTabBarRedDot({ index: 2 });
					res.list.forEach(item => {
						if (item.status === 0) {
							uni.showTabBarRedDot({ index: 2 });
						}
					})
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		toActivity() {
			if (!uni.getStorageSync('token')) {
				this.$Router.push('/pages/common/login');
				return false;
			}
			this.$Router.push('/pages/news/activity');
		},
		taskRefresh() {
			this.getTask();
		},
		goSign() {
			if (this.todayHasSign == 0) {
				this.$Router.pushTab('/pages/integral/index');
			}
		},
		toRevenue() {
			if (!uni.getStorageSync('token')) {
				this.$Router.push('/pages/common/login');
				return false;
			}
			this.$Router.push('/pages/news/revenue');
		}
	},
	onPullDownRefresh() {
		this.refresh();
	}
};
</script>
<style lang="scss" scoped>
.content {
	background: #f6f7fa;
	height: 100vh;
	.top-bar {
		width: 100%;
		height: 60rpx;
		background: #f80916;
	}
	.custom {
		width: 100%;
		height: 280rpx;
		text-align: center;
		position: relative;
		.custom-bj {
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			top: 0;
			z-index: 5;
		}
		.custom-title {
			position: absolute;
			left: 0;
			top: 20rpx;
			width: 100%;
			z-index: 10;
			color: #ffffff;
			font-size: 32rpx;
		}
		.custom-del {
			position: absolute;
			right: 24rpx;
			top: 30rpx;
			z-index: 10;
			color: #ffffff;
			font-size: 37rpx;
			width: 36rpx;
			height: 36rpx;
		}
	}
	.revenue {
		width: 714rpx;
		min-height: 374rpx;
		margin: auto;
		margin-top: -150rpx;
		background: #ffffff;
		border-radius: 20rpx;
		z-index: 10;
		position: relative;
		padding: 36rpx 38rpx;

		.revenue-title {
			@include flexBox(space-between, center);
			&__t {
				color: #171717;
				font-size: 32rpx;
				font-weight: 600;
				display: flex;
				@include flexBox(null, center);
				.blcok {
					display: block;
					width: 6rpx;
					height: 28rpx;
					background: #fe6528;
					border-radius: 4rpx;
					margin-right: 12rpx;
				}
			}
			&__more {
				color: #818181;
				font-size: 28rpx;
			}
		}
		.revenue-list {
			margin-top: 36rpx;
			/deep/.li {
				margin-bottom: 50rpx;
			}
		}
		.iconfont {
			font-size: 28rpx;
		}
	}
	.activity {
		width: 714rpx;
		min-height: 300rpx;
		margin: auto;
		margin-top: 20rpx;
		background: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx 44rpx;
		position: relative;
		.revenue-bj {
			width: 160rpx;
			height: 146rpx;
			position: absolute;
			right: 0;
			bottom: 0;
			z-index: 20;
		}
		.activity-title {
			@include flexBox(space-between, center);
			&__t {
				color: #171717;
				font-size: 32rpx;
				font-weight: 600;
				display: flex;
				@include flexBox(null, center);
				.blcok {
					display: block;
					width: 6rpx;
					height: 28rpx;
					background: #fe6528;
					border-radius: 4rpx;
					margin-right: 12rpx;
				}
			}
			&__more {
				color: #818181;
				font-size: 28rpx;
			}
		}
		.activity-list {
			margin-top: 40rpx;
			/deep/.activity-list__item {
				border-bottom: 2rpx solid #ebebeb;
				&:last-child {
					border-bottom: none;
				}
			}
			/deep/.revenue-list__item {
				margin-top: 36rpx;
			}
		}
		.iconfont {
			font-size: 28rpx;
		}
	}
	.user {
		width: 136rpx;
		height: 78rpx;
		background: #ffffff;
		box-shadow: 0rpx 2rpx 6rpx rgba(0, 0, 0, 0.31);
		opacity: 1;
		border-radius: 38rpx 0rpx 0rpx 38rpx;
		color: #333333;
		font-size: 20rpx;
		position: fixed;
		right: 0;
		bottom: 160rpx;
		@include flexBox(space-around, center);
		.user-img {
			width: 48rpx;
			height: 48rpx;
		}
		.value {
			width: 54rpx;
			display: flex;
			flex-wrap: wrap;
		}
	}
}
</style>
